﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MahApps.Metro.Controls">

    <Style TargetType="{x:Type Button}"
           BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
        <Setter Property="Background"
                Value="{DynamicResource WhiteColorBrush}" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Border"
                            CornerRadius="0"
                            BorderThickness="0"
                            Background="{DynamicResource WhiteColorBrush}"
                            BorderBrush="{DynamicResource WhiteColorBrush}">
                        <ContentPresenter RecognizesAccessKey="True" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused"
                                 Value="true">
                            <Setter TargetName="Border"
                                    Property="BorderBrush"
                                    Value="Transparent" />
                        </Trigger>
                        <Trigger Property="IsMouseOver"
                                 Value="true">
                            <Setter TargetName="Border"
                                    Property="Background"
                                    Value="{DynamicResource GrayBrush8}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="Border" Property="Background"
                                    Value="{DynamicResource GrayBrush6}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter TargetName="Border"
                                    Property="Background"
                                    Value="{DynamicResource GrayBrush10}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate TargetType="{x:Type local:SplitButton}"
                     x:Key="SplitButtonHorizontal">
        <Border Name="PART_Border"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Grid x:Name="PART_Container"
                  Width="{TemplateBinding Width}"
                  Height="{TemplateBinding Height}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0"
                        x:Name="PART_Button"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch"
                        Style="{StaticResource {x:Type Button}}"
                        Command="{TemplateBinding Command}"
                        CommandParameter="{TemplateBinding CommandParameter}"
                        CommandTarget="{TemplateBinding CommandTarget}">
                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}" 
                                VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}">
                        <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding Icon}"></ContentPresenter>
                        <ContentControl MinWidth="20" 
                                        Margin="2"
                                        x:Name="PART_ButtonContent"
                                        HorizontalAlignment="Stretch"
                                        VerticalContentAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}"
                                        VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}"
                                        ContentStringFormat="{TemplateBinding ItemStringFormat}"
                                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        Content="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                        ContentTemplate="{TemplateBinding ItemTemplate}" />
                    </StackPanel>
                </Button>
                <Button Grid.Column="1"
                        x:Name="PART_Expander"
                        VerticalAlignment="Stretch"
                        Width="25"
                        HorizontalAlignment="Stretch"
                        Style="{StaticResource {x:Type Button}}"
                        HorizontalContentAlignment="Center"
                        VerticalContentAlignment="Center">
                    <Button.Content>
                        <Rectangle Name="PART_Rectangle"
                                   Height="6"
                                   Width="10"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Fill="{DynamicResource BlackColorBrush}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill">
                                    <VisualBrush.Visual>
                                        <Canvas Width="18"
                                                Height="18"
                                                Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
                                                UseLayoutRounding="False">
                                            <Path Width="18"
                                                  Height="16"
                                                  Canvas.Left="14.9063"
                                                  Canvas.Top="15.75"
                                                  Stretch="Fill"
                                                  Fill="{DynamicResource BlackColorBrush}"
                                                  Data="F1 M 23.9063,25L 25.9063,23L 32.9063,15.75L 32.9063,22.25L 23.9062,31.75L 14.9063,22.25L 14.9063,15.75L 21.9063,23L 23.9063,25 Z " />
                                        </Canvas>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button.Content>
                </Button>
                <Popup Name="PART_Popup"
                       IsOpen="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                       PlacementTarget="{Binding ElementName=PART_Button}"
                       PopupAnimation="Fade"
                       MinWidth="{TemplateBinding ActualWidth}"
                       StaysOpen="True">
                    <ListBox x:Name="PART_ListBox"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Style="{DynamicResource VirtualisedMetroListBox}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                             ItemTemplate="{TemplateBinding ItemTemplate}"
                             ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                             ItemStringFormat="{TemplateBinding ItemStringFormat}"
                             ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                             ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                             ItemsSource="{TemplateBinding ItemsSource}" />
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True"
                     SourceName="PART_Button">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}"
                        TargetName="PART_Border" />
            </Trigger>
            <Trigger Property="IsFocused"
                     Value="True"
                     SourceName="PART_Expander">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}"
                        TargetName="PART_Border" />
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter TargetName="PART_Rectangle"
                        Property="Fill"
                        Value="{DynamicResource AccentColorBrush}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate TargetType="{x:Type local:SplitButton}"
                     x:Key="SplitButtonVertical">
        <Border Name="PART_Border"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Grid x:Name="PART_Container"
                  Width="{TemplateBinding Width}"
                  Height="{TemplateBinding Height}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Button Grid.Row="0" MinHeight="20"
                        Width="{TemplateBinding Width}"
                        Style="{StaticResource {x:Type Button}}"
                        x:Name="PART_Button"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch"
                        Command="{TemplateBinding Command}"
                        CommandParameter="{TemplateBinding CommandParameter}"
                        CommandTarget="{TemplateBinding CommandTarget}">
                    <Button.Content >
                        <StackPanel Orientation="Vertical"
                                    HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}" 
                                    VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}">
                            <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding Icon}"></ContentPresenter>
                            <ContentControl x:Name="PART_ButtonContent"
                                            Margin="2"
                                            VerticalAlignment="Stretch"
                                            HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}" 
                                            VerticalContentAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource TemplatedParent}}"
                                            ContentStringFormat="{TemplateBinding ItemStringFormat}"
                                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                            Content="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                            ContentTemplate="{TemplateBinding ItemTemplate}" />
                        </StackPanel>
                    </Button.Content>
                </Button>
                <Button x:Name="PART_Expander"
                        Grid.Row="1"
                        Height="20"
                        Style="{StaticResource {x:Type Button}}"
                        HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Center"
                        VerticalContentAlignment="Center">
                    <Button.Content>
                        <Rectangle Name="PART_Rectangle"
                                   Height="6"
                                   Width="10"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Fill="{DynamicResource BlackColorBrush}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill">
                                    <VisualBrush.Visual>
                                        <Canvas Width="18"
                                                Height="18"
                                                Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
                                                UseLayoutRounding="False">
                                            <Path Width="18"
                                                  Height="16"
                                                  Canvas.Left="14.9063"
                                                  Canvas.Top="15.75"
                                                  Stretch="Fill"
                                                  Fill="{DynamicResource BlackColorBrush}"
                                                  Data="F1 M 23.9063,25L 25.9063,23L 32.9063,15.75L 32.9063,22.25L 23.9062,31.75L 14.9063,22.25L 14.9063,15.75L 21.9063,23L 23.9063,25 Z " />
                                        </Canvas>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button.Content>
                </Button>
                <Popup Name="PART_Popup"
                    IsOpen="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                    PlacementTarget="{Binding ElementName=PART_Expander}"
                    PopupAnimation="Fade"
                    MinWidth="{TemplateBinding ActualWidth}"
                    StaysOpen="True">
                    <ListBox x:Name="PART_ListBox"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                             DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                             ItemTemplate="{TemplateBinding ItemTemplate}"
                             ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                             ItemStringFormat="{TemplateBinding ItemStringFormat}"
                             ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                             ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}"
                             ItemsSource="{TemplateBinding ItemsSource}" />
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsFocused"
                     Value="True"
                     SourceName="PART_Button">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}"
                        TargetName="PART_Border" />
            </Trigger>
            <Trigger Property="IsFocused"
                     Value="True"
                     SourceName="PART_Expander">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource AccentColorBrush}"
                        TargetName="PART_Border" />
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter TargetName="PART_Rectangle"
                        Property="Fill"
                        Value="{DynamicResource AccentColorBrush}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="{x:Type local:SplitButton}">
        <Setter Property="MinHeight"
                Value="26" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="FontFamily"
                Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize"
                Value="{DynamicResource ContentFontSize}" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource BlackColorBrush}" />
        <Setter Property="Template"
                Value="{StaticResource SplitButtonHorizontal}" />
        <Style.Triggers>
            <Trigger Property="Orientation"
                     Value="Vertical">
                <Setter Property="Template"
                        Value="{StaticResource SplitButtonVertical}" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>